package com.neu.system.service.impl;

import com.neu.common.annotation.Log;
import com.neu.common.exception.UtilException;
import com.neu.common.utils.DateUtils;
import com.neu.system.constant.RedisConstant;
import com.neu.system.domain.NeuCity;
import com.neu.system.mapper.NeuCityMapper;
import com.neu.system.service.INeuCityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class NeuCityServiceImpl implements INeuCityService {
    @Autowired
    private NeuCityMapper neuCityMapper;

    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 查询【请填写功能名称】
     *
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public NeuCity selectNeuCityById(Long id)
    {
        return neuCityMapper.selectNeuCityById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param neuCity 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<NeuCity> selectNeuCityList(NeuCity neuCity)
    {
        return neuCityMapper.selectNeuCityList(neuCity);
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param neuCity 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertNeuCity(NeuCity neuCity)
    {
        // 判断是否已加入该城市
        String cityName = neuCity.getCity();
        int count = neuCityMapper.selectNeuCityCountByName(cityName);
        if (count > 0) {
            throw new UtilException("已存在该城市，请勿重复添加");
        }

        neuCity.setCreateTime(DateUtils.getNowDate());

        String key = RedisConstant.TODAY_AIR_CITY;
        redisTemplate.delete(key);

        return neuCityMapper.insertNeuCity(neuCity);
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param neuCity 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateNeuCity(NeuCity neuCity)
    {
        neuCity.setUpdateTime(DateUtils.getNowDate());
        String key = RedisConstant.TODAY_AIR_CITY;
        redisTemplate.delete(key);
        return neuCityMapper.updateNeuCity(neuCity);
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteNeuCityByIds(Long[] ids)
    {
        String key = RedisConstant.TODAY_AIR_CITY;
        redisTemplate.delete(key);
        return neuCityMapper.deleteNeuCityByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteNeuCityById(Long id)
    {
        String key = RedisConstant.TODAY_AIR_CITY;
        redisTemplate.delete(key);
        return neuCityMapper.deleteNeuCityById(id);
    }

    @Override
    public Long selectCityByCityName(String name) {
        Long id = neuCityMapper.selectCityByName(name);
        return id;
    }
}
